package com.cq.hd.merchant.controller;

import com.cq.hd.common.annotation.MerchantLoginUser;
import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.IpUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.TbMerchantEnterApplyApi;
import com.cq.hd.member.api.dto.MerchantEnterApplyDto;
import com.cq.hd.member.api.dto.MerchantEnterRepeatApplyDto;
import com.cq.hd.member.api.dto.PreApplyMerchantDto;
import com.cq.hd.member.api.vo.BusinessPageVo;
import com.cq.hd.member.api.vo.MerchantEnterApplyVo;
import com.cq.hd.member.api.vo.PreApplyMerchantVo;
import com.cq.hd.merchant.vo.BusinessInviteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/merchantEnterApply")
@Api(tags = "商户入驻相关接口")
public class MerchantEnterApplyController {

    @Autowired
    private TbMerchantEnterApplyApi merchantEnterApplyApi;

    @Autowired
    private TbBusinessApi businessApi;

    @ApiOperation(value = "商户入驻预申请（供用户点击入驻申请链接调用）")
    @ApiImplicitParam(name = "preApplyMerchantDto", value = "实体", required = true, paramType = "body", dataType = "PreApplyMerchantDto")
    @ApiResponse(response = PreApplyMerchantVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/preApply")
    public Result<PreApplyMerchantVo> preApply(@RequestBody PreApplyMerchantDto preApplyMerchantDto) {
        return merchantEnterApplyApi.preApply(preApplyMerchantDto);
    }

    @ApiOperation(value = "商户入驻申请")
    @ApiImplicitParam(name = "merchantEnterApplyDto", value = "参数", required = true, paramType = "body", dataType = "MerchantEnterApplyDto")
    @PostMapping("/apply")
    public Result<Boolean> apply(@RequestBody MerchantEnterApplyDto merchantEnterApplyDto, HttpServletRequest request) {
        merchantEnterApplyDto.setApplyIp(IpUtil.getIpAddr(request));
        return merchantEnterApplyApi.apply(merchantEnterApplyDto);
    }

    @ApiOperation(value = "根据openid获取商户入驻信息（在登录页面调用，判断是否需要弹入驻成功弹窗）")
    @ApiResponse(response = MerchantEnterApplyVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getEnterInfo")
    public Result<MerchantEnterApplyVo> getEnterInfo(String openid) {
        return merchantEnterApplyApi.getEnterInfoByOpenid(openid);
    }

    @ApiOperation(value = "商户入驻申请新的商家（已经入驻成功，登录后调用）")
    @ApiImplicitParam(name = "merchantEnterRepeatApplyDto", value = "参数", required = true, paramType = "body", dataType = "MerchantEnterRepeatApplyDto")
    @PostMapping("/repeatApply")
    public Result<Boolean> repeatApply(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                       @RequestBody MerchantEnterRepeatApplyDto merchantEnterRepeatApplyDto,
                                       HttpServletRequest request) {
        merchantEnterRepeatApplyDto.setApplyIp(IpUtil.getIpAddr(request));
        merchantEnterRepeatApplyDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        merchantEnterRepeatApplyDto.setOpenid(merchantAppLoginUser.getMerchantUserInfo().getOpenid());
        return merchantEnterApplyApi.repeatApply(merchantEnterRepeatApplyDto);
    }

    @ApiOperation(value = "通过邀请商户的邀请码获取商家部分信息")
    @ApiImplicitParam(name = "settleCode", value = "邀请商户的邀请码", required = true, paramType = "String", dataType = "String")
    @GetMapping("/getByInviteCode")
    public Result<BusinessInviteVo> getBusinessByInviteCode(String settleCode) {
        if (StringUtils.isBlank(settleCode)) {
            Throw.isBusinessException("邀请码不能为空");
        }

        BusinessInviteVo businessInviteVo = new BusinessInviteVo();
        BusinessPageVo businessPageVo = businessApi.getByInviteCode(settleCode).unpack();
        if (businessPageVo != null) {
            BeanUtils.copyProperties(businessPageVo, businessInviteVo);
        }

        return ResultGenerator.success(businessInviteVo);
    }

}
